package com.bjsxt.userscenter.head.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.userscenter.common.controller.BaseController;
import com.bjsxt.userscenter.common.util.ConstatFinalUtil;
import com.bjsxt.userscenter.common.util.EncryptUtil;
import com.bjsxt.userscenter.users.pojo.AUsers;
import com.bjsxt.userscenter.users.service.IUsersService;

@Controller
@RequestMapping("/head/users")
public class UsersHeadController extends BaseController
{
	@Resource
	private IUsersService usersService;

	@RequestMapping("/main")
	public String main()
	{
		ConstatFinalUtil.LOGGER.info("------mian----");		
		return "head/users/main";
	}
	
	@RequestMapping("logout")
	public String logout(HttpServletRequest request,HttpSession session)
	{
		ConstatFinalUtil.LOGGER.info("-----------退出了------------");
		session.removeAttribute("users");
		session.removeAttribute("lastLoginTime");
		
		String token  = request.getParameter("token");
		if(token ==null)
		{
			token="";
		}
		if(!"".equalsIgnoreCase(token))
		{
			ConstatFinalUtil.TOKENS_MAP.remove(token);
		}
		this.info =ConstatFinalUtil.INFO_JSON.getString("8");
		
		session.setAttribute("info", info);
		
		return "redirect:/login.htm";
		
		
	}
	
	/**
	 * 用户修改页面
	 * @return
	 */
	@RequestMapping("/usersUpdate")
	public String usersUpdate(HttpServletRequest request)
	{
		ConstatFinalUtil.LOGGER.info("-----usersUpdate-------");
		HttpSession session = request.getSession();
		AUsers users = (AUsers) session.getAttribute("users");
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", users.getId());
		users = this.usersService.findOneUsersService(condMap);
		
		session.setAttribute("users",users);
		
		
		return "/head/users/usersUpdate";
		
	}
	/**
	 * 用户修改提交页面
	 * @return
	 */
	@RequestMapping("/usersUpdateSubmit")
	public String usersUpdateSubmit(HttpServletRequest request)
	{
		ConstatFinalUtil.LOGGER.info("-----usersUpdateSubmit-------");

		HttpSession session = request.getSession();
		AUsers users = (AUsers) session.getAttribute("users");
		Map<String ,Object> condMap = new HashMap<String, Object>();
		condMap.put("id", users.getId());
		users = this.usersService.findOneUsersService(condMap);
		String operType = request.getParameter("operType");
		
		boolean updateFlag = false;
		
		if("updateInfo".equalsIgnoreCase(operType))
		{
			String qq = request.getParameter("qq");
			String address = request.getParameter("address");
			String regionId = request.getParameter("regionId");
			
			
			ConstatFinalUtil.LOGGER.info("---------！！！！！！！！！！！！！！！"+qq+regionId);
			users.setAddress(address);
			users.setQq(qq);
			//users.setRegionId(Integer.valueOf(regionId));
			
			updateFlag = true;
		}else if("updatePass".equalsIgnoreCase(operType))
		{
			String souPass = request.getParameter("souPass");
			String password = request.getParameter("password");
			if(EncryptUtil.checkPass(souPass, users.getPassword()))
			{
				users.setPassword(EncryptUtil.encodeStr(password));
				updateFlag = true ; 
			}else
			{
				info = "原密码不正确";
				
			}
		}else
		{
			info  = "请开始你的操作";
		}
		if(updateFlag)
		{
			users.setUpdateTime(new Date());
			JSONObject json = usersService.updateOneUsersService(users);
			info =json.getString("info");
		}
		request.setAttribute("info", info);
		
		
		return this.usersUpdate(request) ; 
		
	}
	
}
